package com.example.homework.h2021_04_28.fragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.homework.R;
import com.example.homework.h2021_04_28.fragment.adapter.viewholder.CircleViewHolder;
import com.example.homework.h2021_04_28.fragment.bean.CircleBean;

import java.util.ArrayList;
import java.util.List;

public class CircleAdapter extends RecyclerView.Adapter<CircleViewHolder> {

    public Context context;

    private List<CircleBean.DataBean.RowsBean> beans;
    public CircleAdapter(Context context){
        this.context = context;
        this.beans = new ArrayList<>();
    }

    //设置数据源
    public void setBeans(List<CircleBean.DataBean.RowsBean> bs,boolean append) {
        if (append) {
            this.beans.addAll(bs);
            //通知新插入的数据改变
            notifyItemInserted(this.beans.size()-bs.size());
        }else {
            this.beans = bs;
            notifyDataSetChanged();
        }

    }

    //创建每一个列表项
    @NonNull
    @Override
    public CircleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_article_single,parent,false);
        return new CircleViewHolder(view);
    }

    //给列表项赋值
    @Override
    public void onBindViewHolder(@NonNull CircleViewHolder holder, int position) {
        CircleBean.DataBean.RowsBean circleBean = beans.get(position);
        holder.tvTitle.setText(circleBean.getTitle());
        holder.tvAuthor.setText(circleBean.getAuthorName());
        if (circleBean.getThumbnails()!=null && circleBean.getThumbnails().size()>0) {
            Glide.with(context).load(circleBean.getThumbnails())
                    .error(R.drawable.ic_app)
                    .placeholder(R.drawable.ic_app)
                    .into(holder.ivThumb);
        }
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }
}
